<template>
  <a-config-provider :locale="locale" :transformCellText="({ text, column, record, index }) => text||'--'">
    <router-view />
  </a-config-provider>
</template>

<script>
import zh_CN from 'ele-admin-pro/packages/lang/zh_CN';
import zh_TW from 'ele-admin-pro/packages/lang/zh_TW';
import en from 'ele-admin-pro/packages/lang/en_US';

const languages = { zh_CN, zh_TW, en };

export default {
  data() {
    return {
      locale: languages[this.$i18n.locale]
    };
  },
  computed: {
    language() {
      return this.$i18n.locale;
    }
  },
  watch: {
    language() {
      this.locale = languages[this.$i18n.locale];
    }
  },

  created() {
    console.log(localStorage.getItem('closeTime'));
    console.log(new Date().getTime() - localStorage.getItem('closeTime'));
    //判断是窗口关闭还是刷新
    if (!localStorage.getItem('closeTime')
      ||
      new Date().getTime() - localStorage.getItem('closeTime') >= 5000) {
      // 大于0.5秒就算是关闭了 清除缓存的token
      // localStorage.removeItem(this.tokenStoreName);
      this.$store.dispatch('user/setToken').then(() => {
        // location.replace('/login'); // 这样跳转避免再次登录重复注册动态路由
      });
    }
  },

  mounted() {
    window.addEventListener('beforeunload', this.handleBeforeUnload);

  },
  unmounted() {
    window.removeEventListener('beforeunload', this.handleBeforeUnload);
  },

  methods: {
    handleBeforeUnload() {
      localStorage.setItem('closeTime', new Date().getTime());// 记录关闭前的时间
    }
  }

};
</script>
